package com.kuaimacode.kframework.admin.controller.content;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.content.HelpService;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.admin.SysAdminUser;
import com.kuaimacode.kframework.mybatis.models.index.Help;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 帮助 前端控制器
 * </p>
 *
 * @author Bean
 * @since 2019-03-15
 */
@Controller
@RequestMapping("/help")
public class HelpController {
    private static final String PAGE_FILE_NAME = "help/";

    @Autowired
    private HelpService helpService;

    @RequestMapping("/index")
    public String queryHelpList(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
                                    @RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
                                    Help help,ModelMap model){

        PageInfo<Help> pageInfo = helpService.findAll(pageNum, pageSize,help);
        model.put("pageInfo", pageInfo);
        model.put("search", help);
        return PAGE_FILE_NAME + "index";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String  add(ModelMap map){
        Help help = new Help();
        map.put("isAdd",true);
        map.put("help",help);
        return PAGE_FILE_NAME+"form";

    }


    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable String id, ModelMap map) {
        Help help = helpService.getById(id);
        map.put("help", help);
        map.put("isAdd", false);
        return PAGE_FILE_NAME+"form";
    }
    @RequestMapping(value = {"/delete/{id}"}, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult delete(@PathVariable String id, ModelMap map) {
        try {
            helpService.removeById(id);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }


    @RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult edit(Help help, Boolean isAdd,ModelMap map) {
        try {
            Subject subject = SecurityUtils.getSubject();
            SysAdminUser principal = (SysAdminUser)subject.getPrincipal();

            if(isAdd){
                // 新建保存
                help.setCreateBy(principal.getUserId());
                helpService.createHelp(help);
            }else{
                // 编辑保存用户
                helpService.updateById(help);
            }
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

}
